//
//  CoreDataHandler.m
//  ContactsCoreData
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 张树林. All rights reserved.
//

#import "SLCoreDataHandler.h"
#import "AppDelegate.h"
#import "Contact.h"
#import "SLPerson.h"

@implementation SLCoreDataHandler

+ (NSMutableArray *)selectAll
{
    AppDelegate *manager = [UIApplication sharedApplication].delegate;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Contact"];
    
    // 添加约束 谓词
    NSPredicate *predicate = nil;
    
    request.predicate = predicate;
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [manager.managedObjectContext executeFetchRequest:request error:&error];
    
    NSMutableArray *marray = [array mutableCopy];
    
    return marray;
}


+ (NSMutableArray *)selectWithName:(NSString *)name
{
    AppDelegate *manager = [UIApplication sharedApplication].delegate;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Contact"];
    
    // 添加约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name" , @"%@"], name];
    
    request.predicate = predicate;
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [manager.managedObjectContext executeFetchRequest:request error:&error];
    
    NSMutableArray *marray = [array mutableCopy];
    
    return marray;
}


+ (void)addWithContact:(SLPerson *)person
{
    AppDelegate *manager = [UIApplication sharedApplication].delegate;
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Contact" inManagedObjectContext:manager.managedObjectContext];
    
    
    // 创建实体
    // 参数1: 实体描述
    // 参数2: 数据管理器
    
    Contact *con = [[Contact alloc] initWithEntity:entity insertIntoManagedObjectContext:manager.managedObjectContext];
    
    con.name = person.name;
    con.number = person.number;
    con.phoneNumber = person.phoneNumber;
    con.sex = person.sex;
    con.address = person.address;
    con.qq = person.qq;
    
    [manager saveContext];
}


+ (void)updateWithContact:(SLPerson *)person
{
    AppDelegate *manager = [UIApplication sharedApplication].delegate;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Contact"];
    
    // 添加约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"number" , @"%@"], person.number];
    
    request.predicate = predicate;
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [manager.managedObjectContext executeFetchRequest:request error:&error];
    
    for (Contact *contactTemp in array) {
        if ([contactTemp.number isEqualToString:person.number] ) {
            contactTemp.name = person.name;
            contactTemp.sex = person.sex;
            contactTemp.address = person.address;
            contactTemp.phoneNumber = person.phoneNumber;
            contactTemp.qq = person.qq;
        }
    }
    
    [manager saveContext];
}


+ (void)deleteWithContact:(SLPerson *)person
{
    AppDelegate *manager = [UIApplication sharedApplication].delegate;
    
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Contact"];
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"张"];
    
    request.predicate = predicate;
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [manager.managedObjectContext executeFetchRequest:request error:&error];
    
    for (Contact *contactTemp in array) {
        if ([contactTemp.number isEqualToString:person.number] ) {
            [manager.managedObjectContext deleteObject:contactTemp];
        }
    }
    
    [manager saveContext];
}


+ (BOOL)hasContact:(SLPerson *)person
{
    NSMutableArray *marray = [SLCoreDataHandler selectAll];
    
    for (SLPerson *p in marray) {
        if ([person.number isEqualToString:p.number]) {
            return YES;
        }
    }
    
    return NO;
}

@end
